package com.self.masterwork;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 作者：tianjiayuan
 * 创建时间：2018-05-31 13:31
 * 类描述：
 * 修改人：
 * 修改时间：
 */
public class InBusTransport {

    private ThreadPoolExecutor executor = null;

    public void transport(String key) {
        if (executor == null) {
            executor = new ThreadPoolExecutor(5, 10, 10000, TimeUnit.MICROSECONDS, new ArrayBlockingQueue<Runnable>(100));
        }
        executor.execute(new SendBus(key));
    }

    private class SendBus implements Runnable {

        private String key;
        private int count = 10;

        public SendBus(String key) {
            this.key = key;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "" + Thread.currentThread().getId() + "####################start");
            while (count-- > 0) {
                System.out.println( Thread.currentThread().getName() + "" + Thread.currentThread().getId() + "key-->" + this.key);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("####################################end");
        }
    }
}
